package com.ruijie.acl.task;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

import org.dom4j.Element;
import org.springframework.util.Assert;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.Ace;
import com.ruijie.acl.AclPlanInterface;
import com.ruijie.acl.TimeRange;
import com.ruijie.acl.task.model.AccessGroupInfo;
import com.ruijie.acl.task.model.AceInfo;
import com.ruijie.acl.task.model.AclDeviceInfo;
import com.ruijie.acl.task.model.AclInterfaceInfo;
import com.ruijie.acl.task.model.TimeInfo;
import com.ruijie.acl.task.model.TimeRangeInfo;
import com.ruijie.acl.type.AceTypeEnum;
import com.ruijie.acl.type.EthernetProtocolTypeEnum;
import com.ruijie.acl.type.EthernetTypeEnum;
import com.ruijie.acl.type.MacMatchTypeEnum;
import com.ruijie.acl.type.MatchTypeEnum;
import com.ruijie.acl.utils.AclConfigUtil;
import com.ruijie.acl.utils.AclUtil;
import com.ruijie.emp.event.EventHandleObject;
import com.ruijie.emp.event.support.EventPublisher;
import com.ruijie.emp.task.util.TaskUtil;
import com.ruijie.foundation.adapter.parser.ParseInfoUtil;
import com.ruijie.foundation.adapter.parser.Parser;
import com.ruijie.foundation.adapter.utils.ParserUtil;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.security.AppUserContext;

/**
 * 
 * <p>
 * Title: TaskAclUtil
 * </p>
 * <p>
 * Description: 先放在这里，如果普遍可用，再移到EMP中
 * 
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-7-21
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * 
 * @author ASAI
 */
public class TaskAclUtil {

	// Fa 0/2 --》Fa0/2
	// Fa0/2 --》Fa0/2
	// GigabitEthernet 4/21 --> Gi4/21
	public static String getSubIfName(String ifFullName) { 
		if (ifFullName == null)
			return null;

		// 接口名中不含空格，无法处理

		if (ifFullName.indexOf(" ") < 0) {
			return ifFullName;
		}

		// 按空格分隔进行处理

		ParserUtil parserUtil = new ParserUtil();
		if (parserUtil.addRegexp(ifFullName, "ifname", "(\\S+)\\s*?(\\d+\\S*)")) {
			String pre = parserUtil.get("ifname.1");
			String idx = parserUtil.get("ifname.2");

			return pre.length() > 2 ? pre.substring(0, 2) + idx : pre + idx;
		}

		return ifFullName;
	}

	/**
	 * 
	 * <p>
	 * Description: 发布告警事件
	 * </p>
	 * <p>
	 * Create Time: 2009-7-21
	 * </p>
	 * 
	 * @author chenxiaoyuer
	 * @param eventPublisher
	 * @param ip
	 * @param paras
	 */
	@SuppressWarnings("unchecked")
	public static void publishEvent(EventPublisher eventPublisher, String oid, String ip, Map paras) {
		Assert.notNull(eventPublisher, "eventPublisher is null!");
		Assert.notNull(oid, "oid is null!");

		EventHandleObject eventObject = new EventHandleObject();
		eventObject.setIp(ip);
		eventObject.setOid(oid);// TrapOid.DeviceConfigChanged.getOid());
		eventObject.setEventTime(AppUserContext.getCurrentUserLocalTime());
		if (paras != null) {
			for (Iterator it = paras.entrySet().iterator(); it.hasNext();) {
				Entry entry = (Entry) it.next();
				eventObject.putParameters((String) entry.getKey(), entry.getValue());
			}
		}

		eventPublisher.publishEvent(eventObject);
	}

	public static String getChildrenText(Element el, String childName) {
		StringBuffer sb = new StringBuffer();

		if (el != null) {
			List<?> children = null;
			if (childName != null) {
				children = el.selectNodes(childName);
			} else {
				children = el.elements();
			}
			if (children != null) {
				for (Iterator<?> it = children.iterator(); it.hasNext();) {
					Element child = (Element) it.next();
					sb.append(child.getText()).append(",");
				}
			}
		}

		return sb.toString();
	}

	/**
	 * 把取得的字符串，变成ACLDeviceInfo类型。
	 * 
	 * 
	 * 
	 * @param desc
	 *            字符串
	 * 
	 * 
	 * @return ACL设备信息
	 */
	public static AclDeviceInfo convertAclDeviceInfo(String desc) {

		AclDeviceInfo aclDeviceInfo = new AclDeviceInfo();
		// 处理返回结果
		Parser parser = new Parser();

		try {
			boolean parseFlag = ParseInfoUtil.parseInfo(parser, desc, TaskAclDomain.ACL_PARSER_XML, "acldevice", null);

			if (!parseFlag) {
				throw new BusinessException("acl_parse_error");
			}
		} catch (Exception e) {
			throw new BusinessException("acl_parse_error");
		}

		// ACL 时间段

		aclDeviceInfo.setTimeRangeInfoList(convertTimeRangeInfoList(parser));
		// ACL 组

		ArrayList<AccessGroupInfo> accessGroupList = new ArrayList<AccessGroupInfo>();
		// 标准ACL规则
		accessGroupList.addAll(convertStandardAceInfoList(parser));
		// 扩展ACL规则
		accessGroupList.addAll(convertExtendedAceInfoList(parser));
		// MAC扩展ACL规则
		accessGroupList.addAll(convertMacAceInfoList(parser));
		// 专家扩展ACL规则
		accessGroupList.addAll(convertExpertAceInfoList(parser));
		aclDeviceInfo.setAccessGroupInfoList(accessGroupList);

		// ACL 接口
		aclDeviceInfo.setAclInterfaceInfoList(convertAclInterfaceInfo(parser));

		return aclDeviceInfo;
	}

	/**
	 * 从适配器中转换时间段信息。
	 * 
	 * 
	 * 
	 * @param parser
	 *            适配器
	 * 
	 * 
	 * @return 时间段信息列表
	 * 
	 * 
	 */
	public static List<TimeRangeInfo> convertTimeRangeInfoList(Parser parser) {
		String key = "";
		// ACL 时间段

		List<TimeRangeInfo> timeRangeInfoList = new ArrayList<TimeRangeInfo>();
		key = "";
		if (parser.getParser().has("timerange.count")) {
			int count = parseInt(parser.getParser().get("timerange.count"));
			for (int i = 0; i < count; i++) {
				TimeRangeInfo timeRangeInfo = new TimeRangeInfo();
				key = "timerange." + i + ".0";

				if (parser.getParser().has(key)) {
					String timerangeConfig = parser.getParser().get(key);
					Parser parserTimerangeInfo = new Parser();
					ParseInfoUtil.parseInfo(parserTimerangeInfo, timerangeConfig, TaskAclDomain.ACL_PARSER_XML,
							"timerangeinfo", null);

					key = "timerangename.1";
					// Timerange Name 时间段名称

					if (parserTimerangeInfo.getParser().has(key)) {
						String timerangeName = parserTimerangeInfo.getParser().get(key);
						timeRangeInfo.setName(timerangeName);
					}

					ArrayList<TimeInfo> timeInfoList = new ArrayList<TimeInfo>();

					// Timeinfo 时间信息
					if (parserTimerangeInfo.getParser().has("timeinfo.count")) {
						int tiCount = parseInt(parserTimerangeInfo.getParser().get("timeinfo.count"));
						for (int j = 0; j < tiCount; j++) {
							TimeInfo timeInfo = new TimeInfo();
							key = "timeinfo." + j + ".1";
							timeInfo.setPeriodic(parserTimerangeInfo.getParser().get(key));
							key = "timeinfo." + j + ".3";
							timeInfo.setStartTime(parserTimerangeInfo.getParser().get(key));
							key = "timeinfo." + j + ".4";
							timeInfo.setEndTime(parserTimerangeInfo.getParser().get(key));
							timeInfoList.add(timeInfo);
						}
						timeRangeInfo.setTimeInfoList(timeInfoList);
						timeRangeInfoList.add(timeRangeInfo);
					}
				}
			}
		}
		return timeRangeInfoList;
	}


	
	/**
	 * 从适配器中转换时间段信息。
	 * 
	 * 
	 * 
	 * @param desc
	 *            时间段字符串
	 * @param name
	 *            名称
	 * @return 时间段信息
	 * 
	 * 
	 */
	public static TimeRangeInfo convertTimeRangeInfo(String desc, String name) {
		Assert.hasText(name);
		TimeRangeInfo timeRangeInfo = new TimeRangeInfo();
		timeRangeInfo.setName(name);
		if (!AclUtil.empty(desc)) {
			// ACL 时间段

			String key = "";
			Parser parserTimerangeInfo = new Parser();
			ParseInfoUtil.parseInfo(parserTimerangeInfo, desc, TaskAclDomain.ACL_PARSER_XML, "timerangeinfo", null);
			ArrayList<TimeInfo> timeInfoList = new ArrayList<TimeInfo>();
			
			//绝对时间信息
			key = "absolutetimeinfo.1";  // 绝对时间信息类型
			if (parserTimerangeInfo.getParser().has(key)) {
				String absoluteType = parserTimerangeInfo.getParser().get(key);
				key = "absolutetimeinfo.2";  // 绝对时间信息时间
				String absoluteTime = parserTimerangeInfo.getParser().get(key);
				
				key = "absolutetimeinfo.3";  // 绝对时间信息日
				String absoluteDay = parserTimerangeInfo.getParser().get(key);
				
				key = "absolutetimeinfo.4";  // 绝对时间信息月
				String absoluteMonth = parserTimerangeInfo.getParser().get(key);
				
				key = "absolutetimeinfo.5";  // 绝对时间信息年
				String absoluteYear = parserTimerangeInfo.getParser().get(key);
				
				Date date = ConvertDate(absoluteYear,absoluteMonth,absoluteDay,absoluteTime);
				
				if (absoluteType.trim().toLowerCase().equals("start")){
					timeRangeInfo.setAbsoluteStartDate(date);
					key = "absolutetimeinfo.6";  
					if (parserTimerangeInfo.getParser().has(key)) {
						
						key = "absolutetimeinfo.7";  // 绝对时间信息时间
						absoluteTime = parserTimerangeInfo.getParser().get(key);
						key = "absolutetimeinfo.8";  // 绝对时间信息日
						absoluteDay = parserTimerangeInfo.getParser().get(key);
						key = "absolutetimeinfo.9";  // 绝对时间信息月
						absoluteMonth = parserTimerangeInfo.getParser().get(key);
						key = "absolutetimeinfo.10";  // 绝对时间信息年
						absoluteYear = parserTimerangeInfo.getParser().get(key);
						Date endDate = ConvertDate(absoluteYear,absoluteMonth,absoluteDay,absoluteTime);
						timeRangeInfo.setAbsoluteEndDate(endDate);

					}
				}else{
					timeRangeInfo.setAbsoluteEndDate(date);
				}				
			}
			
			// Timeinfo 时间信息
			if (parserTimerangeInfo.getParser().has("timeinfo.count")) {
				int tiCount = parseInt(parserTimerangeInfo.getParser().get("timeinfo.count"));
				for (int j = 0; j < tiCount; j++) {
					TimeInfo timeInfo = new TimeInfo();
					key = "timeinfo." + j + ".1";
					timeInfo.setPeriodic(parserTimerangeInfo.getParser().get(key));
					key = "timeinfo." + j + ".3";
					timeInfo.setStartTime(parserTimerangeInfo.getParser().get(key));
					key = "timeinfo." + j + ".4";
					timeInfo.setEndTime(parserTimerangeInfo.getParser().get(key));
					timeInfoList.add(timeInfo);
				}
				timeRangeInfo.setTimeInfoList(timeInfoList);

			}
		}
		return timeRangeInfo;
	}

	/**
	 * 从适配器中转换标准规则信息。
	 * 
	 * 
	 * 
	 * @param parser
	 *            适配器
	 * 
	 * 
	 * @return 标准规则信息列表
	 */
	public static List<AccessGroupInfo> convertStandardAceInfoList(Parser parser) {
		// 标准ACL规则
		String key = "";
		ArrayList<AccessGroupInfo> accessGroupList = new ArrayList<AccessGroupInfo>();
		if (parser.getParser().has("standardaccessgroup.count")) {
			int count = parseInt(parser.getParser().get("standardaccessgroup.count"));
			for (int i = 0; i < count; i++) {
				AccessGroupInfo standardAccessGroup = new AccessGroupInfo();

				key = "standardaccessgroup." + i + ".1";
				if (parser.getParser().has(key)) {

					String timerangeConfig = parser.getParser().get(key);
					Parser parserAccessGroupInfo = new Parser();
					ParseInfoUtil.parseInfo(parserAccessGroupInfo, timerangeConfig, TaskAclDomain.ACL_PARSER_XML,
							"standardacl", null);

					key = "aclname.1";
					// Access List Name ACL组名称

					if (parserAccessGroupInfo.getParser().has(key)) {
						String accessGroupName = parserAccessGroupInfo.getParser().get(key);
						standardAccessGroup.setName(accessGroupName);
					}

					// Access List 的类型

					standardAccessGroup.setAclType(AceTypeEnum.STANDARD.toString());

					ArrayList<AceInfo> aceInfoList = new ArrayList<AceInfo>();

					// ACE 规则信息
					int aCount = parseInt(parserAccessGroupInfo.getParser().get("aceinfo.count"));

					for (int j = 0; j < aCount; j++) {
						AceInfo aceInfo = new AceInfo();
						// 规则号

						key = "aceinfo." + j + ".1";
						aceInfo.setAceNum(parserAccessGroupInfo.getParser().get(key));

						// 动作
						key = "aceinfo." + j + ".2";
						aceInfo.setAceAction(parserAccessGroupInfo.getParser().get(key));

						// 匹配类型
						key = "aceinfo." + j + ".3";
						String matchType = parserAccessGroupInfo.getParser().get(key);
						// 如为所有

						if (matchType.equals("any")) {
							aceInfo.setSourceMatchType("any");
						}
						// 如果为HOST
						else if (matchType.equals("host")) {
							aceInfo.setSourceMatchType("host");
							key = "aceinfo." + j + ".4";
							aceInfo.setSourceAddr(parserAccessGroupInfo.getParser().get(key));
						}
						// 如果为网段

						else {
							aceInfo.setSourceMatchType("segment");
							aceInfo.setSourceAddr(matchType);
							key = "aceinfo." + j + ".4";
							aceInfo.setSourceWildcard(parserAccessGroupInfo.getParser().get(key));
						}

						// 时间段处理

						key = "aceinfo." + j + ".6";

						if (parserAccessGroupInfo.getParser().has(key)) {
							aceInfo.setTimeRangeName(parserAccessGroupInfo.getParser().get(key));
						}
						aceInfoList.add(aceInfo);

					}
					standardAccessGroup.setAceInfoList(aceInfoList);
					accessGroupList.add(standardAccessGroup);
				}
			}
		}
		return accessGroupList;
	}

	/**
	 * 从适配器中转换标准规则信息。
	 * 
	 * 
	 * 
	 * @param parser
	 *            适配器
	 * 
	 * 
	 * @return 标准规则信息
	 */
	public static AccessGroupInfo convertStandardAceInfo(String desc) {

		AccessGroupInfo standardAccessGroup = new AccessGroupInfo();
		Parser parserAccessGroupInfo = new Parser();
		ParseInfoUtil.parseInfo(parserAccessGroupInfo, desc, TaskAclDomain.ACL_PARSER_XML, "standardacl", null);

		String key = "aclname.1";
		// Access List Name ACL组名称

		if (parserAccessGroupInfo.getParser().has(key)) {
			String accessGroupName = parserAccessGroupInfo.getParser().get(key);
			standardAccessGroup.setName(accessGroupName);
		}

		// Access List 的类型

		standardAccessGroup.setAclType(AceTypeEnum.STANDARD.toString());

		ArrayList<AceInfo> aceInfoList = new ArrayList<AceInfo>();

		// ACE 规则信息
		int aCount = parseInt(parserAccessGroupInfo.getParser().get("aceinfo.count"));

		for (int j = 0; j < aCount; j++) {
			AceInfo aceInfo = new AceInfo();
			// 规则号

			key = "aceinfo." + j + ".1";
			aceInfo.setAceNum(parserAccessGroupInfo.getParser().get(key));

			// 动作
			key = "aceinfo." + j + ".2";
			aceInfo.setAceAction(parserAccessGroupInfo.getParser().get(key));

			// 匹配类型
			key = "aceinfo." + j + ".3";
			String matchType = parserAccessGroupInfo.getParser().get(key);
			// 如为所有

			if (matchType.equals("any")) {
				aceInfo.setSourceMatchType("any");
			}
			// 如果为HOST
			else if (matchType.equals("host")) {
				aceInfo.setSourceMatchType("host");
				key = "aceinfo." + j + ".4";
				aceInfo.setSourceAddr(parserAccessGroupInfo.getParser().get(key));
			}
			// 如果为网段

			else {
				aceInfo.setSourceMatchType("segment");
				aceInfo.setSourceAddr(matchType);
				key = "aceinfo." + j + ".4";
				aceInfo.setSourceWildcard(parserAccessGroupInfo.getParser().get(key));
			}

			// 时间段处理

			key = "aceinfo." + j + ".6";

			if (parserAccessGroupInfo.getParser().has(key)) {
				aceInfo.setTimeRangeName(parserAccessGroupInfo.getParser().get(key));
			}
			aceInfoList.add(aceInfo);

		}
		standardAccessGroup.setAceInfoList(aceInfoList);
		return standardAccessGroup;
	}

	/**
	 * 从适配器中转换扩展规则信息。
	 * 
	 * 
	 * 
	 * @param parser
	 *            适配器
	 * 
	 * 
	 * @return 扩展规则信息列表
	 */
	public static List<AccessGroupInfo> convertExtendedAceInfoList(Parser parser) {
		// 标准ACL规则
		String key = "";
		ArrayList<AccessGroupInfo> accessGroupList = new ArrayList<AccessGroupInfo>();
		if (parser.getParser().has("extendedaccessgroup.count")) {
			int count = parseInt(parser.getParser().get("extendedaccessgroup.count"));
			for (int i = 0; i < count; i++) {
				AccessGroupInfo standardAccessGroup = new AccessGroupInfo();

				key = "extendedaccessgroup." + i + ".0";
				if (parser.getParser().has(key)) {
					String timerangeConfig = parser.getParser().get(key);
					Parser parserAccessGroupInfo = new Parser();
					ParseInfoUtil.parseInfo(parserAccessGroupInfo, timerangeConfig, TaskAclDomain.ACL_PARSER_XML,
							"extendedacl", null);

					key = "aclname.1";
					// Access List Name ACL组名称

					if (parserAccessGroupInfo.getParser().has(key)) {
						String accessGroupName = parserAccessGroupInfo.getParser().get(key);
						standardAccessGroup.setName(accessGroupName);
					}

					// Access List 的类型

					standardAccessGroup.setAclType(AceTypeEnum.EXTENDED.toString());

					ArrayList<AceInfo> aceInfoList = new ArrayList<AceInfo>();

					// ACE 规则信息
					int aCount = parseInt(parserAccessGroupInfo.getParser().get("aceinfo.count"));

					for (int j = 0; j < aCount; j++) {
						AceInfo aceInfo = new AceInfo();
						// 规则号

						key = "aceinfo." + j + ".1";
						aceInfo.setAceNum(parserAccessGroupInfo.getParser().get(key));

						// 动作
						key = "aceinfo." + j + ".2";
						aceInfo.setAceAction(parserAccessGroupInfo.getParser().get(key));

						// 协议
						key = "aceinfo." + j + ".3";
						aceInfo.setProtocolType(parserAccessGroupInfo.getParser().get(key));

						// 源匹配类型

						key = "aceinfo." + j + ".4";
						String sourceMatchType = parserAccessGroupInfo.getParser().get(key);
						// 如为所有

						if (sourceMatchType.indexOf("any") >= 0) {
							aceInfo.setSourceMatchType("any");
						}
						// 如果为HOST
						else if (sourceMatchType.indexOf("host") >= 0) {
							aceInfo.setSourceMatchType("host");
							String[] arrTemp = sourceMatchType.trim().split(" ");
							// key = "aceinfo." + j + ".5";
							if (arrTemp.length == 2) {
								aceInfo.setSourceAddr(arrTemp[1]);
							}
						}
						// 如果为网段

						else {
							aceInfo.setSourceMatchType("segment");
							String[] arrTemp = sourceMatchType.trim().split(" ");
							// key = "aceinfo." + j + ".5";
							if (arrTemp.length == 2) {
								aceInfo.setSourceAddr(arrTemp[0]);
								aceInfo.setSourceWildcard(arrTemp[1]);
							}
							// key = "aceinfo." + j + ".5";
							// aceInfo.setSourceWildcard(parserAccessGroupInfo.getParser().get(key));
						}

						// 源端口

						key = "aceinfo." + j + ".5";
						String sourcePort = parserAccessGroupInfo.getParser().get(key);
						if (sourcePort != null && sourcePort.trim().length() > 2) {
							sourcePort = sourcePort.trim();
							aceInfo.setSourcePortOperator(sourcePort.substring(0, 2));
							sourcePort = sourcePort.substring(2).trim();
							aceInfo.setSourcePort(sourcePort);
						}

						// 目的匹配类型
						key = "aceinfo." + j + ".6";
						String desMatchType = parserAccessGroupInfo.getParser().get(key);
						// 如为所有

						if (desMatchType.indexOf("any") >= 0) {
							aceInfo.setDesMatchType("any");
						}
						// 如果为HOST
						else if (desMatchType.indexOf("host") >= 0) {
							aceInfo.setDesMatchType("host");
							String[] arrTemp = desMatchType.trim().split(" ");
							// key = "aceinfo." + j + ".5";
							if (arrTemp.length == 2) {
								aceInfo.setDesAddr(arrTemp[1]);
							}
						}
						// 如果为网段

						else {
							aceInfo.setDesMatchType("segment");
							String[] arrTemp = desMatchType.trim().split(" ");
							// key = "aceinfo." + j + ".5";
							if (arrTemp.length == 2) {
								aceInfo.setDesAddr(arrTemp[0]);
								aceInfo.setDesWildcard(arrTemp[1]);
							}
						}

						// 目的端口
						key = "aceinfo." + j + ".7";
						String desPort = parserAccessGroupInfo.getParser().get(key);
						if (desPort != null && desPort.trim().length() > 2) {
							desPort = desPort.trim();
							aceInfo.setDesPortOperator(desPort.substring(0, 2));
							desPort = desPort.substring(2).trim();
							aceInfo.setDesPort(desPort);
						}

						// 时间段处理

						key = "aceinfo." + j + ".9";

						if (parserAccessGroupInfo.getParser().has(key)) {
							aceInfo.setTimeRangeName(parserAccessGroupInfo.getParser().get(key));
						}
						aceInfoList.add(aceInfo);

					}
					standardAccessGroup.setAceInfoList(aceInfoList);
					accessGroupList.add(standardAccessGroup);
				}
			}
		}
		return accessGroupList;
	}

	/**
	 * 从适配器中转换扩展规则信息。
	 * 
	 * 
	 * 
	 * @param parser
	 *            适配器
	 * 
	 * 
	 * @return 扩展规则信息
	 */
	public static AccessGroupInfo convertExtendedAceInfo(String desc) {
		AccessGroupInfo standardAccessGroup = new AccessGroupInfo();
		Parser parserAccessGroupInfo = new Parser();
		ParseInfoUtil.parseInfo(parserAccessGroupInfo, desc, TaskAclDomain.ACL_PARSER_XML, "extendedacl", null);

		String key = "aclname.1";
		// Access List Name ACL组名称

		if (parserAccessGroupInfo.getParser().has(key)) {
			String accessGroupName = parserAccessGroupInfo.getParser().get(key);
			standardAccessGroup.setName(accessGroupName);
		}

		// Access List 的类型

		standardAccessGroup.setAclType(AceTypeEnum.EXTENDED.toString());

		ArrayList<AceInfo> aceInfoList = new ArrayList<AceInfo>();

		// ACE 规则信息
		int aCount = parseInt(parserAccessGroupInfo.getParser().get("aceinfo.count"));

		for (int j = 0; j < aCount; j++) {
			AceInfo aceInfo = new AceInfo();
			// 规则号

			key = "aceinfo." + j + ".1";
			aceInfo.setAceNum(parserAccessGroupInfo.getParser().get(key));

			// 动作
			key = "aceinfo." + j + ".2";
			aceInfo.setAceAction(parserAccessGroupInfo.getParser().get(key));

			// 协议
			key = "aceinfo." + j + ".3";
			aceInfo.setProtocolType(parserAccessGroupInfo.getParser().get(key));

			// 源匹配类型

			key = "aceinfo." + j + ".4";
			String sourceMatchType = parserAccessGroupInfo.getParser().get(key);
			// 如为所有

			if (sourceMatchType.indexOf("any") >= 0) {
				aceInfo.setSourceMatchType("any");
			}
			// 如果为HOST
			else if (sourceMatchType.indexOf("host") >= 0) {
				aceInfo.setSourceMatchType("host");
				String[] arrTemp = sourceMatchType.trim().split(" ");
				// key = "aceinfo." + j + ".5";
				if (arrTemp.length == 2) {
					aceInfo.setSourceAddr(arrTemp[1]);
				}
			}
			// 如果为网段

			else {
				aceInfo.setSourceMatchType("segment");
				String[] arrTemp = sourceMatchType.trim().split(" ");
				// key = "aceinfo." + j + ".5";
				if (arrTemp.length == 2) {
					aceInfo.setSourceAddr(arrTemp[0]);
					aceInfo.setSourceWildcard(arrTemp[1]);
				}
				// key = "aceinfo." + j + ".5";
				// aceInfo.setSourceWildcard(parserAccessGroupInfo.getParser().get(key));
			}

			// 源端口

			key = "aceinfo." + j + ".5";
			String sourcePort = parserAccessGroupInfo.getParser().get(key);
			if (sourcePort != null && sourcePort.trim().length() > 2) {
				sourcePort = sourcePort.trim();
				aceInfo.setSourcePortOperator(sourcePort.substring(0, 2));
				sourcePort = sourcePort.substring(2).trim();
				aceInfo.setSourcePort(sourcePort);
			}

			// 目的匹配类型
			key = "aceinfo." + j + ".6";
			String desMatchType = parserAccessGroupInfo.getParser().get(key);
			// 如为所有

			if (desMatchType.indexOf("any") >= 0) {
				aceInfo.setDesMatchType("any");
			}
			// 如果为HOST
			else if (desMatchType.indexOf("host") >= 0) {
				aceInfo.setDesMatchType("host");
				String[] arrTemp = desMatchType.trim().split(" ");
				// key = "aceinfo." + j + ".5";
				if (arrTemp.length == 2) {
					aceInfo.setDesAddr(arrTemp[1]);
				}
			}
			// 如果为网段

			else {
				aceInfo.setDesMatchType("segment");
				String[] arrTemp = desMatchType.trim().split(" ");
				// key = "aceinfo." + j + ".5";
				if (arrTemp.length == 2) {
					aceInfo.setDesAddr(arrTemp[0]);
					aceInfo.setDesWildcard(arrTemp[1]);
				}
			}

			// 目的端口
			key = "aceinfo." + j + ".7";
			String desPort = parserAccessGroupInfo.getParser().get(key);
			if (desPort != null && desPort.trim().length() > 2) {
				desPort = desPort.trim();
				aceInfo.setDesPortOperator(desPort.substring(0, 2));
				desPort = desPort.substring(2).trim();
				aceInfo.setDesPort(desPort);
			}

			// 时间段处理

			key = "aceinfo." + j + ".9";

			if (parserAccessGroupInfo.getParser().has(key)) {
				aceInfo.setTimeRangeName(parserAccessGroupInfo.getParser().get(key));
			}
			aceInfoList.add(aceInfo);

		}
		standardAccessGroup.setAceInfoList(aceInfoList);
		return standardAccessGroup;
	}

	/**
	 * 从适配器中转换MAC扩展规则信息。
	 * 
	 * 
	 * 
	 * @param parser
	 *            适配器
	 * 
	 * 
	 * @return MAC扩展规则信息列表
	 */
	public static List<AccessGroupInfo> convertMacAceInfoList(Parser parser) {
		// 标准ACL规则
		String key = "";
		ArrayList<AccessGroupInfo> accessGroupList = new ArrayList<AccessGroupInfo>();
		if (parser.getParser().has("macaccessgroup.count")) {
			int count = parseInt(parser.getParser().get("macaccessgroup.count"));
			for (int i = 0; i < count; i++) {
				AccessGroupInfo standardAccessGroup = new AccessGroupInfo();

				key = "macaccessgroup." + i + ".0";
				if (parser.getParser().has(key)) {
					String timerangeConfig = parser.getParser().get(key);
					Parser parserAccessGroupInfo = new Parser();
					ParseInfoUtil.parseInfo(parserAccessGroupInfo, timerangeConfig, TaskAclDomain.ACL_PARSER_XML,
							"macacl", null);

					key = "aclname.1";
					// Access List Name ACL组名称

					if (parserAccessGroupInfo.getParser().has(key)) {
						String accessGroupName = parserAccessGroupInfo.getParser().get(key);
						standardAccessGroup.setName(accessGroupName);
					}

					// Access List 的类型

					standardAccessGroup.setAclType(AceTypeEnum.MAC.toString());

					ArrayList<AceInfo> aceInfoList = new ArrayList<AceInfo>();

					// ACE 规则信息
					int aCount = parseInt(parserAccessGroupInfo.getParser().get("aceinfo.count"));

					for (int j = 0; j < aCount; j++) {
						AceInfo aceInfo = new AceInfo();
						// 规则号

						key = "aceinfo." + j + ".1";
						aceInfo.setAceNum(parserAccessGroupInfo.getParser().get(key));

						// 动作
						key = "aceinfo." + j + ".2";
						aceInfo.setAceAction(parserAccessGroupInfo.getParser().get(key));

						// 源匹配类型

						key = "aceinfo." + j + ".3";
						String sourceMacMatchType = parserAccessGroupInfo.getParser().get(key);
						// 如为所有

						if (sourceMacMatchType.equals("any")) {
							aceInfo.setSourceMacMatchType("any");
						}
						// 如果为HOST
						else if (sourceMacMatchType.equals("host")) {
							aceInfo.setSourceMacMatchType("host");
							key = "aceinfo." + j + ".4";
							aceInfo.setSourceMacAddr(parserAccessGroupInfo.getParser().get(key));
						}

						// 目的匹配类型
						key = "aceinfo." + j + ".5";
						String desMacMatchType = parserAccessGroupInfo.getParser().get(key);
						// 如为所有

						if (desMacMatchType.equals("any")) {
							aceInfo.setDesMacMatchType("any");
						}
						// 如果为HOST
						else if (desMacMatchType.equals("host")) {
							aceInfo.setDesMacMatchType("host");
							key = "aceinfo." + j + ".6";
							aceInfo.setDesMacAddr(parserAccessGroupInfo.getParser().get(key));
						}

						// 以太网协议

						key = "aceinfo." + j + ".7";
						aceInfo.setProtocolType(parserAccessGroupInfo.getParser().get(key));

						// 时间段处理

						key = "aceinfo." + j + ".9";

						if (parserAccessGroupInfo.getParser().has(key)) {
							aceInfo.setTimeRangeName(parserAccessGroupInfo.getParser().get(key));
						}
						aceInfoList.add(aceInfo);

					}
					standardAccessGroup.setAceInfoList(aceInfoList);
					accessGroupList.add(standardAccessGroup);
				}
			}
		}
		return accessGroupList;
	}

	/**
	 * 从适配器中转换MAC扩展规则信息。
	 * 
	 * 
	 * 
	 * @param parser
	 *            适配器
	 * 
	 * 
	 * @return MAC扩展规则信息
	 */
	public static AccessGroupInfo convertMacAceInfo(String desc) {

		AccessGroupInfo standardAccessGroup = new AccessGroupInfo();

		Parser parserAccessGroupInfo = new Parser();
		ParseInfoUtil.parseInfo(parserAccessGroupInfo, desc, TaskAclDomain.ACL_PARSER_XML, "macacl", null);

		String key = "aclname.1";
		// Access List Name ACL组名称

		if (parserAccessGroupInfo.getParser().has(key)) {
			String accessGroupName = parserAccessGroupInfo.getParser().get(key);
			standardAccessGroup.setName(accessGroupName);
		}

		// Access List 的类型

		standardAccessGroup.setAclType(AceTypeEnum.MAC.toString());

		ArrayList<AceInfo> aceInfoList = new ArrayList<AceInfo>();

		// ACE 规则信息
		int aCount = parseInt(parserAccessGroupInfo.getParser().get("aceinfo.count"));

		for (int j = 0; j < aCount; j++) {
			AceInfo aceInfo = new AceInfo();
			// 规则号

			key = "aceinfo." + j + ".1";
			aceInfo.setAceNum(parserAccessGroupInfo.getParser().get(key));

			// 动作
			key = "aceinfo." + j + ".2";
			aceInfo.setAceAction(parserAccessGroupInfo.getParser().get(key));

			// 源匹配类型

			key = "aceinfo." + j + ".3";
			String sourceMacMatchType = parserAccessGroupInfo.getParser().get(key);
			// 如为所有

			if (sourceMacMatchType.equals("any")) {
				aceInfo.setSourceMacMatchType("any");
			}
			// 如果为HOST
			else if (sourceMacMatchType.equals("host")) {
				aceInfo.setSourceMacMatchType("host");
				key = "aceinfo." + j + ".4";
				aceInfo.setSourceMacAddr(parserAccessGroupInfo.getParser().get(key));
			}

			// 目的匹配类型
			key = "aceinfo." + j + ".5";
			String desMacMatchType = parserAccessGroupInfo.getParser().get(key);
			// 如为所有

			if (desMacMatchType.equals("any")) {
				aceInfo.setDesMacMatchType("any");
			}
			// 如果为HOST
			else if (desMacMatchType.equals("host")) {
				aceInfo.setDesMacMatchType("host");
				key = "aceinfo." + j + ".6";
				aceInfo.setDesMacAddr(parserAccessGroupInfo.getParser().get(key));
			}

			// 以太网协议

			key = "aceinfo." + j + ".7";
			aceInfo.setProtocolType(parserAccessGroupInfo.getParser().get(key));

			// 时间段处理

			key = "aceinfo." + j + ".9";

			if (parserAccessGroupInfo.getParser().has(key)) {
				aceInfo.setTimeRangeName(parserAccessGroupInfo.getParser().get(key));
			}
			aceInfoList.add(aceInfo);

		}
		standardAccessGroup.setAceInfoList(aceInfoList);
		return standardAccessGroup;
	}

	/**
	 * 从适配器中转换专家扩展规则信息。
	 * 
	 * 
	 * 
	 * @param parser
	 *            适配器
	 * 
	 * 
	 * @return 专家扩展规则信息列表
	 */
	public static List<AccessGroupInfo> convertExpertAceInfoList(Parser parser) {
		// 标准ACL规则
		String key = "";
		ArrayList<AccessGroupInfo> accessGroupList = new ArrayList<AccessGroupInfo>();
		if (parser.getParser().has("expertaccessgroup.count")) {
			int count = parseInt(parser.getParser().get("expertaccessgroup.count"));
			for (int i = 0; i < count; i++) {
				AccessGroupInfo standardAccessGroup = new AccessGroupInfo();

				key = "expertaccessgroup." + i + ".0";
				if (parser.getParser().has(key)) {
					String timerangeConfig = parser.getParser().get(key);
					Parser parserAccessGroupInfo = new Parser();
					ParseInfoUtil.parseInfo(parserAccessGroupInfo, timerangeConfig, TaskAclDomain.ACL_PARSER_XML,
							"expertacl", null);

					key = "aclname.1";
					// Access List Name ACL组名称

					if (parserAccessGroupInfo.getParser().has(key)) {
						String accessGroupName = parserAccessGroupInfo.getParser().get(key);
						standardAccessGroup.setName(accessGroupName);
					}

					// Access List 的类型

					standardAccessGroup.setAclType(AceTypeEnum.EXPERT.toString());
					ArrayList<AceInfo> aceInfoList = new ArrayList<AceInfo>();
					aceInfoList.addAll(convertExpertAceInfo(parserAccessGroupInfo, "protocolaceinfo"));
					aceInfoList.addAll(convertExpertAceInfo(parserAccessGroupInfo, "aceinfo"));
					standardAccessGroup.setAceInfoList(aceInfoList);
					accessGroupList.add(standardAccessGroup);
				}
			}
		}
		return accessGroupList;
	}

	/**
	 * 从适配器中转换专家扩展规则信息。
	 * 
	 * 
	 * 
	 * @param parser
	 *            适配器
	 * 
	 * 
	 * @return 专家扩展规则信息
	 */
	public static AccessGroupInfo convertExpertAceInfo(String desc) {

		AccessGroupInfo standardAccessGroup = new AccessGroupInfo();

		Parser parserAccessGroupInfo = new Parser();
		ParseInfoUtil.parseInfo(parserAccessGroupInfo, desc, TaskAclDomain.ACL_PARSER_XML, "expertacl", null);

		String key = "aclname.1";
		// Access List Name ACL组名称

		if (parserAccessGroupInfo.getParser().has(key)) {
			String accessGroupName = parserAccessGroupInfo.getParser().get(key);
			standardAccessGroup.setName(accessGroupName);
		}
		// Access List 的类型

		standardAccessGroup.setAclType(AceTypeEnum.EXPERT.toString());
		ArrayList<AceInfo> aceInfoList = new ArrayList<AceInfo>();
		//aceInfoList.addAll(convertExpertAceInfo(parserAccessGroupInfo, "protocolaceinfo"));
		aceInfoList.addAll(convertExpertAceInfo(parserAccessGroupInfo, "aceinfo"));
		//aceInfoList.addAll(convertExpertAceInfo4Mac(parserAccessGroupInfo));
		standardAccessGroup.setAceInfoList(aceInfoList);

		return standardAccessGroup;
	}

	/**
	 * 处理专家规则时的ACE信息
	 * 
	 * @param parserAccessGroupInfo
	 *            适配器
	 * 
	 * 
	 * @param name
	 *            名称
	 * @return ACE信息列表
	 */
	public static ArrayList<AceInfo> convertExpertAceInfo4Mac(Parser parserAccessGroupInfo) {
		ArrayList<AceInfo> aceInfoList = new ArrayList<AceInfo>();

		// ACE 规则信息
		int aCount = parseInt(parserAccessGroupInfo.getParser().get("macaceinfo.count"));

		for (int j = 0; j < aCount; j++) {
			AceInfo aceInfo = new AceInfo();
			String key = "";
			// 规则号

			key = "macaceinfo." + j + ".1";
			aceInfo.setAceNum(parserAccessGroupInfo.getParser().get(key));
			// 动作
			key = "macaceinfo." + j + ".2";
			aceInfo.setAceAction(parserAccessGroupInfo.getParser().get(key));
			key = "macaceinfo." + j + ".3";
			String protocolType = parserAccessGroupInfo.getParser().get(key);
			// 如果不是协议，则不处理该条记录。

			if (protocolType.equals("any") || protocolType.equals("host") || protocolType.indexOf("VID") > 0
					|| segmentV4Validator(protocolType)) {
				continue;
			} else {
				aceInfo.setProtocolType(protocolType);
			}

			// VLAN ID
			key = "macaceinfo." + j + ".4";
			String vlanId = parserAccessGroupInfo.getParser().get(key);
			if (vlanId != null && vlanId.trim().length() > 3) {
				vlanId = vlanId.substring(3);
				aceInfo.setVlanNum(vlanId.trim());
			}

			// 源MAC匹配类型
			key = "macaceinfo." + j + ".5";
			String sourceMacMatchType = parserAccessGroupInfo.getParser().get(key);
			// 如为所有

			if (sourceMacMatchType.equals("any")) {
				aceInfo.setSourceMacMatchType("any");
			}
			// 如果为HOST
			else if (sourceMacMatchType.equals("host")) {
				aceInfo.setSourceMacMatchType("host");
				key = "macaceinfo." + j + ".6";
				aceInfo.setSourceMacAddr(parserAccessGroupInfo.getParser().get(key));
			}

			// 目的MAC匹配类型
			key = "macaceinfo." + j + ".7";
			String desMacMatchType = parserAccessGroupInfo.getParser().get(key);
			// 如为所有

			if (desMacMatchType.equals("any")) {
				aceInfo.setDesMacMatchType("any");
			}
			// 如果为HOST
			else if (desMacMatchType.equals("host")) {
				aceInfo.setDesMacMatchType("host");
				key = "macaceinfo." + j + ".8";
				aceInfo.setDesMacAddr(parserAccessGroupInfo.getParser().get(key));
			}

			// 时间段处理

			key = "macaceinfo." + j + ".10";
			if (parserAccessGroupInfo.getParser().has(key)) {
				aceInfo.setTimeRangeName(parserAccessGroupInfo.getParser().get(key));
			}
			aceInfoList.add(aceInfo);
		}
		return aceInfoList;
	}

	/**
	 * 处理专家规则时的ACE信息
	 * 
	 * @param parserAccessGroupInfo
	 *            适配器
	 * 
	 * 
	 * @param name
	 *            名称
	 * @return ACE信息列表
	 */
	public static ArrayList<AceInfo> convertExpertAceInfo(Parser parserAccessGroupInfo, String name) {
		ArrayList<AceInfo> aceInfoList = new ArrayList<AceInfo>();
		int startIndex = 0;
		// ACE中有协议
		//if (name.equals("protocolaceinfo")) {
			startIndex = 1;
		//}
		name = name + ".";
		// ACE 规则信息
		int aCount = parseInt(parserAccessGroupInfo.getParser().get(name + "count"));

		for (int j = 0; j < aCount; j++) {
			AceInfo aceInfo = new AceInfo();
			boolean anyFlag = false;
			boolean macAnyFlag =false;
			boolean macDescFlag =false;
			String key = "";
			// 规则号

			key = name + j + ".1";
			aceInfo.setAceNum(parserAccessGroupInfo.getParser().get(key));

			// 动作
			key = name + j + ".2";
			aceInfo.setAceAction(parserAccessGroupInfo.getParser().get(key));

			key = name + j + ".3";
			String protocolType = parserAccessGroupInfo.getParser().get(key);
			// 如果不是协议，则不处理该条记录。

			//if (name.equals("protocolaceinfo.")
			//		&& (protocolType.equals("any") || protocolType.equals("host") || protocolType.indexOf("VID") > 0 || segmentV4Validator(protocolType))) {
			if (!protocolType.equals("any")) {
				aceInfo.setProtocolType(protocolType);
			}else{
				aceInfo.setSourceMatchType("any");
				anyFlag = true;
			}

			// VLAN ID
			key = name + j + "." + (startIndex + 3);
			String vlanId = parserAccessGroupInfo.getParser().get(key);
			if (vlanId != null && vlanId.trim().length() > 3) {
				vlanId = vlanId.substring(3);
				aceInfo.setVlanNum(vlanId.trim());
			}

			// 源IP匹配类型
			key = name + j + "." + (startIndex + 4);
			String sourceMatchType = parserAccessGroupInfo.getParser().get(key);
			// 如为所有

			if (!anyFlag){
				if (sourceMatchType.indexOf("any") >= 0) {
					aceInfo.setSourceMatchType("any");
				}
				// 如果为HOST
				else if (sourceMatchType.indexOf("host") >= 0) {
	
					aceInfo.setSourceMatchType("host");
					String[] arrTemp = sourceMatchType.trim().split(" ");
					// key = "aceinfo." + j + ".5";
					if (arrTemp.length == 2) {
						aceInfo.setSourceAddr(arrTemp[1]);
					}
				}
				// 如果为网段
	
				else {
					aceInfo.setSourceMatchType("segment");
					String[] arrTemp = sourceMatchType.trim().split(" ");
					// key = "aceinfo." + j + ".5";
					if (arrTemp.length == 2) {
						aceInfo.setSourceAddr(arrTemp[0]);
						aceInfo.setSourceWildcard(arrTemp[1]);
					}
				}
			}else if (sourceMatchType.indexOf("any") >= 0) {
				aceInfo.setSourceMacMatchType("any");
				macAnyFlag = true;
			}
			
			// 源MAC匹配类型
			key = name + j + "." + (startIndex + 5);
			String sourceMacMatchType = parserAccessGroupInfo.getParser().get(key);
			if (!macAnyFlag){
				// 如为所有
				if (!macAnyFlag)
				if (sourceMacMatchType.equals("any")) {
					aceInfo.setSourceMacMatchType("any");
				}
				// 如果为HOST
				else if (sourceMacMatchType.equals("host")) {
					aceInfo.setSourceMacMatchType("host");
					key = name + j + "." + (startIndex + 6);
					aceInfo.setSourceMacAddr(parserAccessGroupInfo.getParser().get(key));
				}
			}else if (sourceMacMatchType.indexOf("any") >= 0) {
				aceInfo.setDesMatchType("any");
				macDescFlag = true;
			}
			// 源端口

			key = name + j + "." + (startIndex + 7);
			String sourcePort = parserAccessGroupInfo.getParser().get(key);
			if (sourcePort != null && sourcePort.trim().length() > 2) {
				sourcePort = sourcePort.trim();
				aceInfo.setSourcePortOperator(sourcePort.substring(0, 2));
				sourcePort = sourcePort.substring(2).trim();
				aceInfo.setSourcePort(sourcePort);
			}

			// 目的IP匹配类型
			key = name + j + "." + (startIndex + 8);
			String desMatchType = parserAccessGroupInfo.getParser().get(key);
			if (!macDescFlag){
				// 如为所有
	
				if (desMatchType.indexOf("any") >= 0) {
					aceInfo.setDesMatchType("any");
				}
				// 如果为HOST
				else if (desMatchType.indexOf("host") >= 0) {
					aceInfo.setDesMatchType("host");
					String[] arrTemp = desMatchType.trim().split(" ");
					// key = "aceinfo." + j + ".5";
					if (arrTemp.length == 2) {
						aceInfo.setDesAddr(arrTemp[1]);
					}
				}
				// 如果为网段
	
				else {
					aceInfo.setDesMatchType("segment");
					String[] arrTemp = desMatchType.trim().split(" ");
					// key = "aceinfo." + j + ".5";
					if (arrTemp.length == 2) {
						aceInfo.setDesAddr(arrTemp[0]);
						aceInfo.setDesWildcard(arrTemp[1]);
					}
				}

			}
			// 目的MAC匹配类型
			key = name + j + "." + (startIndex + 9);
			String desMacMatchType = parserAccessGroupInfo.getParser().get(key);
			// 如为所有

			if (desMacMatchType.equals("any")) {
				aceInfo.setDesMacMatchType("any");
			}
			// 如果为HOST
			else if (desMacMatchType.equals("host")) {
				aceInfo.setDesMacMatchType("host");
				key = name + j + "." + (startIndex + 10);
				aceInfo.setDesMacAddr(parserAccessGroupInfo.getParser().get(key));
			}

			// 目的端口
			key = name + j + "." + (startIndex + 11);
			String desPort = parserAccessGroupInfo.getParser().get(key);
			if (desPort != null && desPort.trim().length() > 2) {
				desPort = desPort.trim();
				aceInfo.setDesPortOperator(desPort.substring(0, 2));
				desPort = desPort.substring(2).trim();
				aceInfo.setDesPort(desPort);
			}

			// 时间段处理

			key = name + j + "." + (startIndex + 13);

			if (parserAccessGroupInfo.getParser().has(key)) {
				aceInfo.setTimeRangeName(parserAccessGroupInfo.getParser().get(key));
			}
			aceInfoList.add(aceInfo);
		}
		return aceInfoList;
	}

	/**
	 * 从适配器中转换ACL接口信息。
	 * 
	 * 
	 * @param parser
	 *            适配器
	 * 
	 * 
	 * @return ACL接口信息列表
	 */
	public static ArrayList<AclInterfaceInfo> convertAclInterfaceInfo(Parser parser) {
		ArrayList<AclInterfaceInfo> aclInterfaces = new ArrayList<AclInterfaceInfo>();
		String key = "";
		if (parser.getParser().has("aclinterfaceinfo.count")) {
			int count = parseInt(parser.getParser().get("aclinterfaceinfo.count"));
			for (int i = 0; i < count; i++) {
				AclInterfaceInfo aclInterfaceInfo = new AclInterfaceInfo();
				key = "aclinterfaceinfo." + i + ".0";
				if (parser.getParser().has(key)) {
					String interfaceInfo = parser.getParser().get(key);
					Parser parserInterface = new Parser();
					ParseInfoUtil.parseInfo(parserInterface, interfaceInfo, TaskAclDomain.ACL_PARSER_XML,
							"aclinterface", null);

					key = "interface.1";
					// 接口名

					if (parserInterface.getParser().has(key)) {
						aclInterfaceInfo.setIfName(TaskAclUtil.getSubIfName(parserInterface.getParser().get(key)));
					}
					key = "inaccessgroup.1";
					// Out 组名称

					if (parserInterface.getParser().has(key)) {
						aclInterfaceInfo.setInAccessGroupName(parserInterface.getParser().get(key));
					}
					key = "outaccessgroup.1";
					// Out 组名称

					if (parserInterface.getParser().has(key)) {
						aclInterfaceInfo.setOutAccessGroupName(parserInterface.getParser().get(key));
					}
					if (!AclUtil.empty(aclInterfaceInfo.getInAccessGroupName())
							|| !AclUtil.empty(aclInterfaceInfo.getOutAccessGroupName())) {
						aclInterfaces.add(aclInterfaceInfo);
					}
				}
			}
		}
		return aclInterfaces;
	}

	/**
	 * 把组转换成字符串
	 * 
	 * @param asscessGroup
	 *            组
	 * 
	 * @return 字符串信息
	 * 
	 */
	public static String accessGroup2String(AccessGroup accessGroup) {
		StringBuffer result = new StringBuffer();
		Assert.notNull(accessGroup);
		Assert.hasText(accessGroup.getName());
		String aclType = accessGroup.getAclType();
		Assert.notNull(aclType);
		String excalmatoryMask = "!";
		String newLine = "\r\n";
		result.append(excalmatoryMask).append(newLine);

		// [start] 标准规则
		if (AceTypeEnum.STANDARD.toString().equals(aclType)) {
			result.append("ip access-list standard ").append(accessGroup.getName()).append(newLine);
			// [start] 规则列表
			for (Ace aceInfo : accessGroup.getAces()) {
				if (aceInfo == null)
					continue;
				StringBuffer aceCommandString = new StringBuffer();
				aceCommandString.append(" ");
				// 动作
				if (!AclUtil.empty(aceInfo.getActionType())) {
					aceCommandString.append(aceInfo.getActionType()).append(" ");
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 匹配类型
				String matchType = aceInfo.getSourceMatchType();
				if (!AclUtil.empty(matchType)) {
					// 所有IP
					if (MatchTypeEnum.any.toString().equals(matchType)) {
						aceCommandString.append(MatchTypeEnum.any.toString().toLowerCase()).append(" ");
					}
					// 主机
					else if (MatchTypeEnum.host.toString().equals(matchType)) {
						aceCommandString.append(MatchTypeEnum.host.toString().toLowerCase()).append(" ").append(
								aceInfo.getSourceAddr()).append(" ");
					}
					// 网段
					else {
						aceCommandString.append(aceInfo.getSourceAddr()).append(" ")
								.append(aceInfo.getSourceWildcard()).append(" ");
					}
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 时间段

				if (aceInfo.getTimeRangeName() != null) {
					aceCommandString.append("time-range ").append(aceInfo.getTimeRangeName());
				} else if (aceInfo.getTimeRange() != null && aceInfo.getTimeRange().getName() != null) {
					aceCommandString.append("time-range ").append(aceInfo.getTimeRange().getName());
				}
				result.append(aceCommandString).append(newLine);
			}
			// [end] 规则列表
		}
		// [end] 标准规则

		// [start] 扩展规则
		else if (AceTypeEnum.EXTENDED.toString().equals(aclType)) {
			result.append("ip access-list extended ").append(accessGroup.getName()).append(newLine);
			// [start] 扩展规则列表
			for (Ace aceInfo : accessGroup.getAces()) {
				if (aceInfo == null)
					continue;
				StringBuffer aceCommandString = new StringBuffer();
				aceCommandString.append(" ");
				// 动作
				if (!AclUtil.empty(aceInfo.getActionType())) {
					aceCommandString.append(aceInfo.getActionType()).append(" ");
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 协议
				if (!AclUtil.empty(aceInfo.getProtocolType())) {
					aceCommandString.append(aceInfo.getProtocolType()).append(" ");
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 源匹配类型

				String sourceMatchType = aceInfo.getSourceMatchType();
				if (!AclUtil.empty(sourceMatchType)) {
					// 所有IP
					if (MatchTypeEnum.any.toString().equals(sourceMatchType)) {
						aceCommandString.append(MatchTypeEnum.any.toString()).append(" ");
					}
					// 主机
					else if (MatchTypeEnum.host.toString().equals(sourceMatchType)) {
						aceCommandString.append(MatchTypeEnum.host.toString()).append(" ").append(
								aceInfo.getSourceAddr()).append(" ");
					}
					// 网段
					else {
						aceCommandString.append(aceInfo.getSourceAddr()).append(" ")
								.append(aceInfo.getSourceWildcard()).append(" ");
					}
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 源端口

				if (aceInfo.getSourcePort() != null) {
					aceCommandString.append("eq ").append(aceInfo.getSourcePort()).append(" ");
				}

				// 目的匹配类型
				String desMatchType = aceInfo.getDesMatchType();
				if (!AclUtil.empty(desMatchType)) {
					// 所有IP
					if (MatchTypeEnum.any.toString().equals(desMatchType)) {
						aceCommandString.append(MatchTypeEnum.any.toString()).append(" ");
					}
					// 主机
					else if (MatchTypeEnum.host.toString().equals(desMatchType)) {
						aceCommandString.append(MatchTypeEnum.host.toString()).append(" ").append(aceInfo.getDesAddr())
								.append(" ");
					}
					// 网段
					else {
						aceCommandString.append(aceInfo.getDesAddr()).append(" ").append(aceInfo.getDesWildcard())
								.append(" ");
					}
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}
				// 目的端口
				if (aceInfo.getDesPort() != null) {
					aceCommandString.append("eq ").append(aceInfo.getDesPort()).append(" ");
				}

				// 时间段

				if (aceInfo.getTimeRangeName() != null) {
					aceCommandString.append("time-range ").append(aceInfo.getTimeRangeName());
				} else if (aceInfo.getTimeRange() != null && aceInfo.getTimeRange().getName() != null) {
					aceCommandString.append("time-range ").append(aceInfo.getTimeRange().getName());
				}
				result.append(aceCommandString).append(newLine);
			}
			// [end] 扩展规则列表
		}
		// [end] 扩展规则

		// [start] MAC扩展规则
		else if (AceTypeEnum.MAC.toString().equals(aclType)) {
			result.append("mac access-list extended ").append(accessGroup.getName()).append(newLine);

			// [start] MAC扩展规则列表
			for (Ace aceInfo : accessGroup.getAces()) {
				if (aceInfo == null)
					continue;
				StringBuffer aceCommandString = new StringBuffer();
				aceCommandString.append(" ");
				// 动作
				if (!AclUtil.empty(aceInfo.getActionType())) {
					aceCommandString.append(aceInfo.getActionType()).append(" ");
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 源MAC匹配类型
				String sourceMacMatchType = aceInfo.getSourceMacMatchType();
				if (!AclUtil.empty(sourceMacMatchType)) {
					// 所有IP
					if (MacMatchTypeEnum.any.toString().equals(sourceMacMatchType)) {
						aceCommandString.append("any ");
					}
					// 主机
					else if (MacMatchTypeEnum.host.toString().equals(sourceMacMatchType)) {
						aceCommandString.append(MacMatchTypeEnum.host.toString()).append(" ").append(
								TaskUtil.transDeviceMac(aceInfo.getSourceMacAddr())).append(" ");
					}
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 目的MAC匹配类型
				String desMacMatchType = aceInfo.getDesMacMatchType();
				if (!AclUtil.empty(desMacMatchType)) {
					// 所有IP
					if (MacMatchTypeEnum.any.toString().equals(desMacMatchType)) {
						aceCommandString.append("any ");
					}
					// 主机
					else if (MacMatchTypeEnum.host.toString().equals(desMacMatchType)) {
						aceCommandString.append(MacMatchTypeEnum.host.toString()).append(" ").append(
								TaskUtil.transDeviceMac(aceInfo.getDesMacAddr())).append(" ");
					}
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 以太网协议,可以为空
				if (!AclUtil.empty(aceInfo.getEthernetType())) {
					aceCommandString.append(EthernetTypeEnum.valueOf(aceInfo.getEthernetType()).value()).append(" ");
				}

				// 时间段

				if (aceInfo.getTimeRangeName() != null) {
					aceCommandString.append("time-range ").append(aceInfo.getTimeRangeName());
				} else if (aceInfo.getTimeRange() != null && aceInfo.getTimeRange().getName() != null) {
					aceCommandString.append("time-range ").append(aceInfo.getTimeRange().getName());
				}
				result.append(aceCommandString).append(newLine);
			}
			// [end] MAC扩展规则列表
		}
		// [end] MAC扩展规则

		// [start] 专家扩展规则
		else if (AceTypeEnum.EXPERT.toString().equals(aclType)) {
			// 下发组信息

			result.append("expert access-list extended ").append(accessGroup.getName()).append(newLine);
			// [start] 专家扩展规则列表
			for (Ace ace : accessGroup.getAces()) {
				if (ace == null)
					continue;
				StringBuffer aceCommandString = new StringBuffer();
				aceCommandString.append(" ");
				// 动作
				if (!AclUtil.empty(ace.getActionType())) {
					aceCommandString.append(ace.getActionType()).append(" ");
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 协议，可以为空

				if (!AclUtil.empty(ace.getProtocolType())) {
					aceCommandString.append(EthernetProtocolTypeEnum.valueOf(ace.getProtocolType()).value())
							.append(" ");
				}

				// VLAN ID，可以为空

				if (!AclUtil.empty(ace.getVlanNum())) {
					aceCommandString.append("VID ").append(ace.getVlanNum()).append(" ");
				}

				// 源IP匹配类型，当协议类型为以太网的时间，为空
				String sourceMatchType = ace.getSourceMatchType();
				if (!AclUtil.empty(sourceMatchType)) {
					// 所有IP
					if (MatchTypeEnum.any.toString().equals(sourceMatchType)) {
						aceCommandString.append(MatchTypeEnum.any.toString()).append(" ");
					}
					// 主机
					else if (MatchTypeEnum.host.toString().equals(sourceMatchType)) {
						aceCommandString.append(MatchTypeEnum.host.toString()).append(" ").append(ace.getSourceAddr())
								.append(" ");
					}
					// 网段
					else {
						aceCommandString.append(ace.getSourceAddr()).append(" ").append(ace.getSourceWildcard())
								.append(" ");
					}
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 源MAC匹配类型，当协议类型为以太网的时间，为空
				String sourceMacMatchType = ace.getSourceMacMatchType();
				if (!AclUtil.empty(sourceMacMatchType)) {
					// 所有IP
					if (MacMatchTypeEnum.any.toString().equals(sourceMacMatchType)) {
						aceCommandString.append("any ");
					}
					// 主机
					else if (MacMatchTypeEnum.host.toString().equals(sourceMacMatchType)) {
						aceCommandString.append(MacMatchTypeEnum.host.toString()).append(" ").append(
								TaskUtil.transDeviceMac(ace.getSourceMacAddr())).append(" ");
					}
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 源端口，可以为空
				if (ace.getSourcePort() != null) {
					aceCommandString.append("eq ").append(ace.getSourcePort().toString()).append(" ");
				}

				// 目的IP匹配类型，当协议类型为以太网的时间，为空
				String desMatchType = ace.getDesMatchType();
				if (!AclUtil.empty(desMatchType)) {
					// 所有IP
					if (MatchTypeEnum.any.toString().equals(desMatchType)) {
						aceCommandString.append(MatchTypeEnum.any.toString()).append(" ");
					}
					// 主机
					else if (MatchTypeEnum.host.toString().equals(desMatchType)) {
						aceCommandString.append(MatchTypeEnum.host.toString()).append(" ").append(ace.getDesAddr())
								.append(" ");
					}
					// 网段
					else {
						aceCommandString.append(ace.getDesAddr()).append(" ").append(ace.getDesWildcard()).append(" ");
					}
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 目的MAC匹配类型
				String desMacMatchType = ace.getDesMacMatchType();
				if (!AclUtil.empty(desMacMatchType)) {
					// 所有IP
					if (MacMatchTypeEnum.any.toString().equals(desMacMatchType)) {
						aceCommandString.append("any ");
					}
					// 主机
					else if (MacMatchTypeEnum.host.toString().equals(desMacMatchType)) {
						aceCommandString.append(MacMatchTypeEnum.host.toString()).append(" ").append(
								TaskUtil.transDeviceMac(ace.getDesMacAddr())).append(" ");
					}
				} else {
					// result.append(aceCommandString).append(newLine);
					// continue;
				}

				// 目的端口，可以为空

				if (ace.getDesPort() != null) {
					aceCommandString.append("eq ").append(ace.getDesPort()).append(" ");
				}

				// 时间段

				if (ace.getTimeRangeName() != null) {
					aceCommandString.append("time-range ").append(ace.getTimeRangeName());
				} else if (ace.getTimeRange() != null && ace.getTimeRange().getName() != null) {
					aceCommandString.append("time-range ").append(ace.getTimeRange().getName());
				}
				result.append(aceCommandString).append(newLine);
			}
			// [end] 专家扩展规则
		}
		// [end] 专家扩展规则
		result.append("!").append(newLine);
		return result.toString();
	}

	/**
	 * 把组转换成字符串
	 * 
	 * @param asscessGroup
	 *            组
	 * 
	 * @return 字符串信息
	 * 
	 */
	public static String accessGroup2TextFile(AccessGroup accessGroup) {
		String result = "";
		Assert.notNull(accessGroup);
		Assert.hasText(accessGroup.getName());
		// [start] 标准规则
		if (accessGroup.getAclType().equals("standard")) {
			result += "ip access-list standard " + accessGroup.getName();
			// [start] 规则列表
			for (Ace aceInfo : accessGroup.getAces()) {
				String aceCommandString = "";
				// 动作
				if (!AclUtil.empty(aceInfo.getActionType())) {
					aceCommandString += aceInfo.getActionType() + " ";
				} else {
					continue;
				}

				// 匹配类型
				String matchType = aceInfo.getSourceMatchType();
				if (!AclUtil.empty(matchType)) {
					// 所有IP
					if (matchType.equals("any")) {
						aceCommandString += "any ";
					}
					// 主机
					else if (matchType.equals("host")) {
						aceCommandString += "host " + aceInfo.getSourceAddr() + " ";
					}
					// 网段
					else {
						aceCommandString += aceInfo.getSourceAddr() + " " + aceInfo.getSourceWildcard() + " ";
					}
				} else {
					continue;
				}

				// 时间段

				if (!AclUtil.empty(aceInfo.getTimeRange().getName())) {
					aceCommandString += "time-range " + aceInfo.getTimeRange().getName();
				}

			}
			// [end] 规则列表
		}
		// [end] 标准规则
		// [start] 扩展规则
		else if (accessGroup.getAclType().equals("extended")) {
			result += "ip access-list extended " + accessGroup.getName();
			// [start] 扩展规则列表
			for (Ace aceInfo : accessGroup.getAces()) {
				String aceCommandString = "";

				// 动作
				if (!AclUtil.empty(aceInfo.getActionType())) {
					aceCommandString += aceInfo.getActionType() + " ";
				} else {
					continue;
				}

				// 协议
				if (!AclUtil.empty(aceInfo.getProtocolType())) {
					aceCommandString += aceInfo.getProtocolType() + " ";
				} else {
					continue;
				}

				// 源匹配类型

				String sourceMatchType = aceInfo.getSourceMatchType();
				if (!AclUtil.empty(sourceMatchType)) {
					// 所有IP
					if (sourceMatchType.equals("any")) {
						aceCommandString += "any ";
					}
					// 主机
					else if (sourceMatchType.equals("host")) {
						aceCommandString += "host " + aceInfo.getSourceAddr() + " ";
					}
					// 网段
					else {
						aceCommandString += aceInfo.getSourceAddr() + " " + aceInfo.getSourceWildcard() + " ";
					}
				} else {
					continue;
				}

				// 源端口

				if (aceInfo.getSourcePort() != null) {
					aceCommandString += "eq " + aceInfo.getSourcePort().toString() + " ";
				}

				// 目的匹配类型
				String desMatchType = aceInfo.getDesMatchType();
				if (!AclUtil.empty(desMatchType)) {
					// 所有IP
					if (desMatchType.equals("any")) {
						aceCommandString += "any ";
					}
					// 主机
					else if (desMatchType.equals("host")) {
						aceCommandString += "host " + aceInfo.getDesAddr() + " ";
					}
					// 网段
					else {
						aceCommandString += aceInfo.getDesAddr() + " " + aceInfo.getDesWildcard() + " ";
					}
				} else {
					continue;
				}

				// 目的端口
				if (aceInfo.getDesPort() != null) {
					aceCommandString += "eq " + aceInfo.getDesPort().toString() + " ";
				}

				// 时间段

				if (!AclUtil.empty(aceInfo.getTimeRange().getName())) {
					aceCommandString += "time-range " + aceInfo.getTimeRange().getName();
				}

			}
			// [end] 扩展规则列表
		}
		// [end] 扩展规则
		// [start] MAC扩展规则
		else if (accessGroup.getAclType().equals("mac")) {

			result += "mac access-list extended " + accessGroup.getName();

			// [start] MAC扩展规则列表
			for (Ace aceInfo : accessGroup.getAces()) {
				String aceCommandString = "";
				// 动作
				if (!AclUtil.empty(aceInfo.getActionType())) {
					aceCommandString += aceInfo.getActionType() + " ";
				} else {
					continue;
				}

				// 源MAC匹配类型
				String sourceMacMatchType = aceInfo.getSourceMacMatchType();
				if (!AclUtil.empty(sourceMacMatchType)) {
					// 所有IP
					if (sourceMacMatchType.equals("any")) {
						aceCommandString += "any ";
					}
					// 主机
					else if (sourceMacMatchType.equals("host")) {
						aceCommandString += "host " + aceInfo.getSourceMacAddr() + " ";
					}
				} else {
					continue;
				}

				// 目的MAC匹配类型
				String desMacMatchType = aceInfo.getDesMacMatchType();
				if (!AclUtil.empty(desMacMatchType)) {
					// 所有IP
					if (desMacMatchType.equals("any")) {
						aceCommandString += "any ";
					}
					// 主机
					else if (desMacMatchType.equals("host")) {
						aceCommandString += "host " + aceInfo.getDesMacAddr() + " ";
					}
				} else {
					continue;
				}

				// 以太网协议

				if (!AclUtil.empty(aceInfo.getProtocolType())) {
					aceCommandString += aceInfo.getProtocolType() + " ";
				} else {
					continue;
				}

				// 时间段

				if (!AclUtil.empty(aceInfo.getTimeRange().getName())) {
					aceCommandString += "time-range " + aceInfo.getTimeRange().getName();
				}

			}
			// [end] MAC扩展规则列表
		}
		// [end] MAC扩展规则
		// [start] 专家扩展规则
		else if (accessGroup.getAclType().equals("expert")) {
			// 下发组信息

			result += "expert access-list extended " + accessGroup.getName();
			// [start] 专家扩展规则列表
			for (Ace aceInfo : accessGroup.getAces()) {
				String aceCommandString = "";
				// 动作
				if (!AclUtil.empty(aceInfo.getActionType())) {
					aceCommandString += aceInfo.getActionType() + " ";
				} else {
					continue;
				}

				// 协议
				if (!AclUtil.empty(aceInfo.getProtocolType())) {
					aceCommandString += aceInfo.getProtocolType() + " ";
				} else {
					continue;
				}

				// VLAN ID
				if (!AclUtil.empty(aceInfo.getVlanNum())) {
					aceCommandString += "VID " + aceInfo.getVlanNum() + " ";
				} else {
					continue;
				}

				// 源IP匹配类型
				String sourceMatchType = aceInfo.getSourceMatchType();
				if (!AclUtil.empty(sourceMatchType)) {
					// 所有IP
					if (sourceMatchType.equals("any")) {
						aceCommandString += "any ";
					}
					// 主机
					else if (sourceMatchType.equals("host")) {
						aceCommandString += "host " + aceInfo.getSourceAddr() + " ";
					}
					// 网段
					else {
						aceCommandString += aceInfo.getSourceAddr() + " " + aceInfo.getSourceWildcard() + " ";
					}
				} else {
					continue;
				}

				// 源MAC匹配类型
				String sourceMacMatchType = aceInfo.getSourceMacMatchType();
				if (!AclUtil.empty(sourceMacMatchType)) {
					// 所有IP
					if (sourceMacMatchType.equals("any")) {
						aceCommandString += "any ";
					}
					// 主机
					else if (sourceMacMatchType.equals("host")) {
						aceCommandString += "host " + aceInfo.getSourceMacAddr() + " ";
					}
				} else {
					continue;
				}

				// 源端口

				if (aceInfo.getSourcePort() != null) {
					aceCommandString += "eq " + aceInfo.getSourcePort().toString() + " ";
				}

				// 目的IP匹配类型
				String desMatchType = aceInfo.getDesMatchType();
				if (!AclUtil.empty(desMatchType)) {
					// 所有IP
					if (desMatchType.equals("any")) {
						aceCommandString += "any ";
					}
					// 主机
					else if (desMatchType.equals("host")) {
						aceCommandString += "host " + aceInfo.getDesAddr() + " ";
					}
					// 网段
					else {
						aceCommandString += aceInfo.getDesAddr() + " " + aceInfo.getDesWildcard() + " ";
					}
				} else {
					continue;
				}

				// 目的MAC匹配类型
				String desMacMatchType = aceInfo.getDesMacMatchType();
				if (!AclUtil.empty(desMacMatchType)) {
					// 所有IP
					if (desMacMatchType.equals("any")) {
						aceCommandString += "any ";
					}
					// 主机
					else if (desMacMatchType.equals("host")) {
						aceCommandString += "host " + aceInfo.getDesMacAddr() + " ";
					}
				} else {
					continue;
				}

				// 目的端口
				if (aceInfo.getDesPort() != null) {
					aceCommandString += "eq " + aceInfo.getDesPort().toString() + " ";
				}

				// 时间段

				if (!AclUtil.empty(aceInfo.getTimeRange().getName())) {
					aceCommandString += "time-range " + aceInfo.getTimeRange().getName();
				}
			}
			// [end] 专家扩展规则
		}
		// [end] 专家扩展规则
		return result;
	}

	public static int parseInt(String s) {
		try {
			return Integer.valueOf(s.trim());
		} catch (NumberFormatException ne) {
			return -1;
		}
	}

	/**
	 * 验证是否为网段
	 * 
	 * 
	 * 
	 * 
	 * 
	 * @param segment
	 * @return
	 */
	public static boolean segmentV4Validator(String segment) {
		if (segment == null)
			return false;
		String[] sip = segment.split("\\.");
		if (sip.length != 4)
			return false;
		if (segment.lastIndexOf('.') == segment.getBytes().length - 1)
			return false;

		int sip1 = parseInt(sip[0]);
		int sip2 = parseInt(sip[1]);
		int sip3 = parseInt(sip[2]);
		int sip4 = parseInt(sip[3]);

		if (sip1 == 0 && sip2 == 0 && sip3 == 0 && sip4 == 0)
			return true;
		if (sip1 <= 0 || sip1 >= 255)
			return false;
		if (sip2 < 0 || sip3 < 0 || sip2 >= 255 || sip3 >= 255)
			return false;
		if (sip4 < 0 || sip4 >= 255)
			return false;
		return true;
	}

	// private static void

	/**
	 * 从文本文件中读取信息中
	 * 
	 * 
	 * 
	 * 
	 * 
	 * @throws IOException
	 */
	public static String readFromTxtFile(String filePath) throws IOException {
		StringBuffer buffer = new StringBuffer();
		File file = new File(filePath);
		String line; // 用来保存每行读取的内容

		BufferedReader reader = new BufferedReader(new FileReader(file));
		line = reader.readLine(); // 读取第一行

		while (line != null) { // 如果 line 为空说明读完了

			buffer.append(line); // 将读到的内容添加到 buffer 中

			buffer.append("\n"); // 添加换行符

			line = reader.readLine(); // 读取下一行

		}
		return buffer.toString();
	}

	public static void main(String args[]) {
		try {
			String deviceInfo = readFromTxtFile("d:/a.txt");
			convertAclDeviceInfo(deviceInfo);
		} catch (Exception ex) {
			//ex.printStackTrace();
			// System.out.println(ex.getStackTrace())
		}
	}

	/**
	 * 把时间段转换成文本信息
	 * 
	 * 
	 * 
	 * 
	 * 
	 * @param timeRanges
	 * @return
	 */
	public static String timeRange2String(TimeRange timeRange) {
		Assert.notNull(timeRange);
		String newLine = "\r\n";
		StringBuffer result = new StringBuffer();
		result.append("!").append(newLine);
		result.append("time-range ").append(timeRange.getName()).append(newLine);
		
		String absoluteString = " absolute";
		// 绝对时间信息
		if (timeRange.getStartDate() != null){
			absoluteString += " start " + AclConfigUtil.getAbsoluteDateString(timeRange.getStartDate(),false);
		} 
		
		if (timeRange.getEndDate() != null){
			absoluteString += " end " + AclConfigUtil.getAbsoluteDateString(timeRange.getEndDate(),false);
		}
		if(!" absolute".equals(absoluteString)){
			result.append(absoluteString).append(newLine);
		}
		for (com.ruijie.acl.TimeInfo timeInfo : timeRange.getTimeInfos()) {
			if (timeInfo == null)
				continue;
			result.append(" periodic ").append(timeInfo.getPeriodic().replaceAll(",", " ")).append(" ").append(
					timeInfo.getStarttime()).append(" ").append("to ").append(timeInfo.getEndtime()).append(newLine);
		}
		result.append("!").append(newLine);
		return result.toString();
	}

	/**
	 * 把AclPlanInterface的部署情况转换成指令文本
	 * 
	 * @param aclPlanInterface
	 * @return
	 */
	// interface FastEthernet 0/1
	// ip access-group hello11231231 in
	// ip access-group hello1 out
	public static String aclPlanInterface2String(AclPlanInterface aclPlanInterface) {
		Assert.notNull(aclPlanInterface);
		StringBuffer result = new StringBuffer();
		String newLine = "\r\n";
		result.append("!").append(newLine);
		result.append("interface ").append(aclPlanInterface.getDeviceInterface().getName()).append(newLine);
		if (aclPlanInterface.getInAccessGroup() != null) {
			result.append(" ip access-group ").append(aclPlanInterface.getInAccessGroup().getName()).append(" in")
					.append(newLine);
		}
		if (aclPlanInterface.getOutAccessGroup() != null) {
			result.append(" ip access-group ").append(aclPlanInterface.getOutAccessGroup().getName()).append(" out")
					.append(newLine);
		}
		result.append("!").append(newLine);
		return result.toString();

	}

	/**
	 * 取得ACL组列表
	 * 
	 * 
	 * 
	 * @param parser
	 *            正则
	 * @param typeName
	 *            组类型
	 * 
	 * 
	 * @param device
	 *            设备
	 * @return
	 */
	public static List<AccessGroupInfo> loadAccessGroupInfo(Parser parser, String typeName) {
		ArrayList<AccessGroupInfo> accessGroupList = new ArrayList<AccessGroupInfo>();
		if (parser.getParser().has(typeName + ".count")) {
			int count = TaskAclUtil.parseInt(parser.getParser().get(typeName + ".count"));
			for (int i = 0; i < count; i++) {
				String key = typeName + "." + i + ".1";
				if (parser.getParser().has(key)) {
					// PlanParameters parameters = new PlanParameters();
					String value = parser.getParser().get(key);
					// parameters.putParameter("accessGroupName", value);
					AccessGroupInfo accessGroupInfo = new AccessGroupInfo();
					accessGroupInfo.setName(value);
					accessGroupInfo.setAclType(typeName.toUpperCase());
					accessGroupList.add(accessGroupInfo);
					// 发送命令时，会发送前一个的命令。放弃该种方式。

					// parameters.putParameter("aclType", typeName);
					// ItemProcessResult resultAccessGroup = new
					// TaskLoadAccessGroupInfo().processItem(device,
					// parameters);
					// if (resultAccessGroup != null &&
					// resultAccessGroup.getSuccess()) {
					// AccessGroupInfo accessGroupInfo = (AccessGroupInfo)
					// resultAccessGroup.getResultObject();
					// accessGroupList.add(accessGroupInfo);
					// }
				}
			}

		}
		return accessGroupList;
	}

	/**
	 * 取得ACL组列表
	 * 
	 * 
	 * 
	 * @param parser
	 *            正则
	 * @param typeName
	 *            组类型
	 * 
	 * 
	 * @param device
	 *            设备
	 * @return
	 */
	public static List<TimeRangeInfo> loadTimeRangeInfo(Parser parser) {

		ArrayList<TimeRangeInfo> timeRangeInfoList = new ArrayList<TimeRangeInfo>();
		if (parser.getParser().has("timerange.count")) {
			int count = TaskAclUtil.parseInt(parser.getParser().get("timerange.count"));
			for (int i = 0; i < count; i++) {
				String key = "timerange." + i + ".1";
				if (parser.getParser().has(key)) {
					// PlanParameters parameters = new PlanParameters();
					String value = parser.getParser().get(key);
					TimeRangeInfo timeRangeInfo = new TimeRangeInfo();
					timeRangeInfo.setName(value);
					timeRangeInfoList.add(timeRangeInfo);
					// 发送命令时，会发送前一个的命令。放弃该种方式。

					// parameters.putParameter("timeRangeName", value);
					// ItemProcessResult resultTimeRanage = new
					// TaskLoadTimeRangeInfo().processItem(device, parameters);
					// if (resultTimeRanage != null &&
					// resultTimeRanage.getSuccess()) {
					// TimeRangeInfo timeRangeInfo = (TimeRangeInfo)
					// resultTimeRanage.getResultObject();
					// timeRangeInfoList.add(timeRangeInfo);
					// }
				}
			}
		}
		return timeRangeInfoList;
	}

	public static void loadAceInfo(String desc, List<AccessGroupInfo> accessGroupInfoList) {
		if (!AclUtil.empty(desc) && accessGroupInfoList != null && accessGroupInfoList.size() > 0) {
			Map<String,Integer> accessGroupNameMap = new HashMap<String,Integer>();
			//int firstFromPos = 0;
			for (int i = 0; i < accessGroupInfoList.size(); i++) {
				AccessGroupInfo accessGroupInfo = accessGroupInfoList.get(i);
				String aclType = accessGroupInfo.getAclType();
				String prefix = toAccessGroupString(aclType, accessGroupInfo.getName());
				int pos = 0;
				// 如果已经存在
				if (accessGroupNameMap.containsKey(prefix)){
					int firstFromPos = accessGroupNameMap.get(prefix);
					pos = desc.indexOf(prefix,firstFromPos);
				}else{
					pos = desc.indexOf(prefix);
				}
				accessGroupNameMap.put(prefix, pos + prefix.length());
				accessGroupInfo.setIndex(pos);
			}
			Collections.sort(accessGroupInfoList, new Comparator<AccessGroupInfo>() {
				public int compare(AccessGroupInfo obj1, AccessGroupInfo obj2) {
					return obj1.getIndex() - obj2.getIndex();
				}
			});

			for (int i = 0; i < accessGroupInfoList.size(); i++) {
				AccessGroupInfo accessGroupInfo = accessGroupInfoList.get(i);
				String aclType = accessGroupInfo.getAclType();
				int toPos = -1;
				if (i + 1 == accessGroupInfoList.size()) {
					toPos = desc.indexOf("\n!", accessGroupInfo.getIndex());
					if (toPos == -1) {
						toPos = desc.length();
					}
				} else {
					toPos = accessGroupInfoList.get(i + 1).getIndex();
				}
				String aceInfoString = "";
				if(accessGroupInfo.getIndex()!=-1 &&  toPos!=-1){
					aceInfoString = desc.substring(accessGroupInfo.getIndex(), toPos);
				}
				// 标准
				if (aclType.toLowerCase().equals("standard")) {
					accessGroupInfo = TaskAclUtil.convertStandardAceInfo(aceInfoString);
				} else if (aclType.toLowerCase().equals("extended")) {
					accessGroupInfo = TaskAclUtil.convertExtendedAceInfo(aceInfoString);
				} else if (aclType.toLowerCase().equals("mac")) {
					accessGroupInfo = TaskAclUtil.convertMacAceInfo(aceInfoString);
				} else if (aclType.toLowerCase().equals("expert")) {
					accessGroupInfo = TaskAclUtil.convertExpertAceInfo(aceInfoString);
				}
				accessGroupInfoList.set(i, accessGroupInfo);
			}
		}
	}

	public static void loadTimeInfo(String desc, List<TimeRangeInfo> timeRangeInfoList) {
		if (!AclUtil.empty(desc) && timeRangeInfoList != null && timeRangeInfoList.size() > 0) {
			int firstFromPos = 0;
			for (int i = 0; i < timeRangeInfoList.size(); i++) {
				TimeRangeInfo timeRangeInfo = timeRangeInfoList.get(i);
				String prefix = "\ntime-range " + timeRangeInfo.getName() + "\n";
				String suffix = "\n!";
				// 如果最后一个

				if (i + 1 < timeRangeInfoList.size()) {
					suffix = "\ntime-range " + timeRangeInfoList.get(i + 1).getName();
				}
				int fromPos = desc.indexOf(prefix, firstFromPos);
				int toPos = desc.indexOf(suffix, fromPos + prefix.length());
				if (toPos == -1) {
					toPos = desc.length();
				}
				firstFromPos = toPos;
				String timeInfoString = "";
				if(fromPos!=-1 &&  toPos!=-1){
					timeInfoString = desc.substring(fromPos, toPos);
				}
				timeRangeInfoList.set(i, TaskAclUtil.convertTimeRangeInfo(timeInfoString, timeRangeInfo.getName()));
			}
		}
	}

	public static String toAccessGroupString(String aclType, String name) {
		String result = "";
		// 标准
		if (aclType.toLowerCase().equals("standard")) {
			result = "\nip access-list standard ";
		} else if (aclType.toLowerCase().equals("extended")) {
			result = "\nip access-list extended ";
		} else if (aclType.toLowerCase().equals("mac")) {
			result = "\nmac access-list extended ";
		} else if (aclType.toLowerCase().equals("expert")) {
			result = "\nexpert access-list extended ";
		}
		return result + name + "\n";
	}
	/**
	 * 转换成时间
	 * @param year 年
	 * @param month 月
	 * @param day 日
	 * @param time 时间
	 * @return 时间
	 */
	private static Date ConvertDate(String year,String month,String day,String time){
		try{
			// 月份是否为数字
			String format = "yyyy-MM-dd HH:mm";
			try{
				Integer.parseInt(month);
			}catch(Exception ex){
				format = "yyyy-MMM-dd HH:mm";
			}
			SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.ENGLISH);
			return sdf.parse(year + "-" + month + "-" + day + " " + time);
		}catch(Exception ex){
			return null;
		}
	}
}
